import * as _DROPDATA from './DROPDATA'
// import DROPDATA_FILTER from './DROPDATA_FILTER'
// import DROPDATA_CHILDREN_MAP from './DROPDATA_CHILDREN_MAP'
const dataSource = window.ext_industry
let objectKey = []
let DROPDATA = {},DROPDATA_FILTER = {
  salary:["_"],
  work_year: ["-"]
},DROPDATA_CHILDREN_MAP = []

for(var key in dataSource) {
  objectKey.push(key)
}

$.map(objectKey, function (data, index) {
  if(data === 'work_year') {
    var work_year = dataSource.work_year
    $.map(work_year, function (i, k) {
      if(k>0){
        var str = i.code.toString() + '_' + "1000"
        DROPDATA_FILTER.work_year.push(str)
      }
    })
  }
  if(data === 'salary') {
    var salary = dataSource.salary
    $.map(salary, function (i, k) {
      if(k>0){
        DROPDATA_FILTER.salary.push(i.name_fmt)
      }
    })
  }
  DROPDATA[data] = toArray(dataSource[objectKey[index]])
})

var typeCache = []
DROPDATA.company_type = []
DROPDATA.company_industry = []

$.map(dataSource.company_type, function(item) {
  if (item.parent_id != '0' &&  $.inArray(item.parent_id, typeCache) < 0) {
    typeCache.push(item.parent_id)
  }
  DROPDATA.company_type.push([item.code, item.name])
})

$.map(dataSource.company_industry, function(item, index) {
  if ($.inArray(item.id, typeCache) >= 0) {
    DROPDATA.company_industry.push([item.code, item.name, $.inArray(item.id, typeCache)+1])
  } else {
    DROPDATA.company_industry.push([item.code, item.name])
  }
})

var _Cache = []
$.map(dataSource.company_type, function(item) {
  if ($.inArray(item.parent_id, _Cache) < 0) {
    _Cache.push(item.parent_id)
    DROPDATA_CHILDREN_MAP.push([])
  }
  DROPDATA_CHILDREN_MAP[$.inArray(item.parent_id, _Cache)].push(item.code)
})



function array_index(dp) {
  var data = {},
    i, len, list, item, key;

  for (key in dp) {
    data[key] = {};
    list = dp[key];

    for (i = 0, len = list.length; i < len; i++) {
      item = list[i];
      data[key][item[0]] = item[1];
    }
  }
  return data;
}

function toArray(item) {
  let data = item || []
  let resule = []
  $.map(data, function (d, k) {
    resule.push([d.code.toString(), d.name, d.parent_id.toString()])
  })
  return resule
}

let DROPDATA_INDEX = array_index(DROPDATA)

// console.log(window.ext_industry)

// console.log(DROPDATA)

export {
  DROPDATA,
  DROPDATA_FILTER,
  DROPDATA_CHILDREN_MAP,
  DROPDATA_INDEX
};
